package com.ssy.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.ssy.common.CodeMsg;
import com.ssy.common.PageInfo;
import com.ssy.common.Result;
import com.ssy.dao.ClassDao;
import com.ssy.entity.Class;
import com.ssy.service.ClassService;
import com.ssy.vo.ClassVo;
import com.ssy.vo.StudentVo;

import java.util.HashMap;

/**
 * ClassServiceImpl
 *
 * @author ssy
 * @date 2020/11/18
 **/
public class ClassServiceImpl implements ClassService {

    ClassDao classDao = new ClassDao();

    @Override
    public Result selectStudentList(HashMap<String, Object> param, String page, String limit) {
        Integer pageNo = NumberUtil.isNumber(page) ? Integer.parseInt(page) : 1;
        limit = String.valueOf(Integer.MAX_VALUE);
        Integer limitNo = Integer.parseInt(limit);
        PageInfo<StudentVo> studentPageInfo = this.classDao.selectStudentPage(param, pageNo, limitNo);
        return new Result(studentPageInfo);
    }

    @Override
    public Result deleteOneClass(String id) {
        boolean flag = this.classDao.deleteClasss(Integer.parseInt(id));
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result BathdeleteClass(String[] ids) {
        boolean flag = this.classDao.batchDeleteClasss(ids);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result updateClass1(String cId, String number, String cname, String inTime, String majorId, String sum, String instructor) {
        Class class1 = this.classDao.selectClassById(cId);

        //班级号不相等就查
        if (class1.getNumber() != Integer.parseInt(number)) {
            Class class2 = this.classDao.selectClassByNumber(number);
            if (class2 != null) {
                return new Result(CodeMsg.CLASS_NUMBER_ALREADY_EXISTS);
            }
        }

        if (!class1.getCname().equalsIgnoreCase(cname)) {
            Class class3 = this.classDao.selectClassByCname(cname);
            if (class3 != null) {
                return new Result(CodeMsg.CLASS_NAME_ALREADY_EXISTS);
            }
        }

        boolean flag = this.classDao.modifyClass(number, cname, Integer.parseInt(majorId), sum, inTime, instructor, Integer.parseInt(cId));
        return flag ? new Result(CodeMsg.UPDATE_SUCCES) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result addClass(String number, String cname, String inTime, String majorId, String sum, String instructor) {
        Class class1 = this.classDao.selectClassByNumber(number);
        if (class1 != null) {
            return new Result(CodeMsg.CLASS_NUMBER_ALREADY_EXISTS);
        }

        Class class2 = this.classDao.selectClassByCname(cname);
        if (class2 != null) {
            return new Result(CodeMsg.CLASS_NAME_ALREADY_EXISTS);
        }

        boolean flag = this.classDao.insertClass(number, cname, Integer.parseInt(majorId), sum, inTime, instructor);
        return flag ? new Result(CodeMsg.ADD_SUCCES) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result queryPage(HashMap<String, Object> param, String page, String limit) {
        Integer pageNo = NumberUtil.isNumber(page) ? Integer.parseInt(page) : 1;
        Integer limitNo = NumberUtil.isNumber(limit) ? Integer.parseInt(limit) : 10;
        PageInfo<ClassVo> classPageInfo = this.classDao.selectPage(param, pageNo, limitNo);
        return new Result(classPageInfo);
    }
}
